package com.wootion.cims.module.communitypersonal.controller;

import com.alibaba.fastjson.JSONObject;
import com.wootion.cims.general.utils.EasyExcelOrderListener;
import com.wootion.cims.module.communitypersonal.model.InportLog;
import com.wootion.cims.module.communitypersonal.model.InportLogItem;
import com.wootion.cims.module.communitypersonal.model.Person;
import com.wootion.cims.module.communitypersonal.model.PersonQuery;
import com.wootion.cims.module.communitypersonal.service.PersonService;
import com.wootion.cims.module.systemsetting.dictionary.model.LogAnotion;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;


@RestController
@RequestMapping("/person")
@Api(value = "社区个人管理")
@RequiresAuthentication
public class SysPersonController {

    @Resource
    PersonService personService;

    @PostMapping("/save")
    @LogAnotion(type = "新增",value = "1",incident = "个人信息",prefixContent = "")
    @ApiOperation(value = "新增", httpMethod = "POST", notes = "")
    public ResponseResult save(@RequestBody JSONObject jsonObject) {
        if (!Optional.ofNullable(jsonObject.getString("name")).isPresent() ||
                !Optional.ofNullable(jsonObject.getString("nation")).isPresent() ||
                !Optional.ofNullable(jsonObject.getString("tel")).isPresent() ||
                !Optional.ofNullable(jsonObject.getString("idNumber")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.insert(jsonObject);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\",\"password\":\"123456\",\"role\":\"741\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"741\",\"852\"],\"username\":\"741852\"}")
    public ResponseResult update(HttpServletRequest request,@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.getString("name")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.update(jsonObject,request);
    }

    @PostMapping("/delete")
    @LogAnotion(type = "删除",value = "1",incident = "个人信息",prefixContent = "")
    @ApiOperation(value = "删除", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\"}")
    public ResponseResult delete(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.getString("id")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.deleteById(jsonObject.getString("id"));
    }

    @RequiresAuthentication
    @PostMapping("/deletes")
    @LogAnotion(type = "批量删除",value = "1",incident = "个人信息",prefixContent = "删除信息")
    @ApiOperation(value = "批量删除", httpMethod = "POST", notes = "{\"ids\":[\"255762da-3072-4b60-898b-5bc8142250c6\"]}")
    public ResponseResult deletes(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.get("ids")).isPresent()) {
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.deleteByIds(jsonObject);
    }

//    @RequiresAuthentication
//    @PostMapping(value = "/list")
//    @ApiOperation(value = "用户管理-列表查询", httpMethod = "POST", notes = "")
//    public ResponseResult list(@RequestParam(required = false,defaultValue = "") String condition,
//                               @RequestParam(value = "page",defaultValue = "1") Integer page,
//                               @RequestParam(value = "limit",defaultValue = "18") Integer limit) {
//
//        return personService.selectPage(page, limit, condition);
//    }
//
    @RequiresAuthentication
    @PostMapping(value = "/list")
    @ApiOperation(value = "用户管理-列表查询", httpMethod = "POST", notes = "")
    public ResponseResult list(@RequestBody JSONObject condition) {
        JSONObject condition1 = condition.getJSONObject("condition");
        String s = JSONObject.toJSONString(condition1);
        return personService.selectPage(condition.getInteger("page"),condition.getInteger("limit"),s);
    }

    @PostMapping(path = "/findbyid")
    @ApiOperation(value = "根据id查询", httpMethod = "POST", notes = "")
    public ResponseResult findbyid(@RequestParam String id){
        if (!Optional.ofNullable(id).isPresent() || StringUtil.isNullOrEmpty(id)){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }
        return personService.findById(id);
    }

    @PostMapping(path = "/findbycardid")
    @ApiOperation(value = "根据身份证号查询", httpMethod = "POST", notes = "")
    public ResponseResult findbycardid(@RequestParam String card) {
        if (!Optional.ofNullable(card).isPresent() || StringUtil.isNullOrEmpty(card)){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }
        return personService.findByCard(card);
    }

    @ApiOperation(value = "导出", httpMethod = "POST")
    @LogAnotion(type = "导出",value = "1",incident = "个人信息",prefixContent = "执行导出操作")
    @PostMapping(path = "/export")
    public ResponseResult export(@RequestBody JSONObject jsonObject) throws Exception {
       return personService.export(jsonObject);
    }

    @ApiOperation(value = "导入", httpMethod = "POST")
    @LogAnotion(type = "导入",value = "1",incident = "个人信息",prefixContent = "执行导入操作")
    @PostMapping(path = "/inport")
    public ResponseResult inport(MultipartFile file,String abode,String communityId,String plotId){
        return personService.inport(file,abode,communityId,plotId);
    }

    @RequiresAuthentication
    @PostMapping(value = "/inportlist")
    @ApiOperation(value = "数据导入-列表查询", httpMethod = "POST", notes = "")
    public ResponseResult inportlist(@RequestBody JSONObject condition) {
        JSONObject condition1 = condition.getJSONObject("condition");
        String s = JSONObject.toJSONString(condition1);
        return personService.selectPage2(condition.getInteger("page"),condition.getInteger("limit"),s);
    }

    @RequiresAuthentication
    @PostMapping(value = "/inlist")
    @ApiOperation(value = "导入人员-列表查询", httpMethod = "POST", notes = "")
    public ResponseResult inlist(@RequestBody JSONObject condition) {
        JSONObject condition1 = condition.getJSONObject("condition");
        String s = JSONObject.toJSONString(condition1);
        return personService.selectPage3(condition.getInteger("page"),condition.getInteger("limit"),s);
    }

    @RequiresAuthentication
    @PostMapping("/inputs")
    @LogAnotion(type = "批量转入",value = "1",incident = "个人信息",prefixContent = "转入信息")
    @ApiOperation(value = "批量转入", httpMethod = "POST", notes = "{\"ids\":[\"255762da-3072-4b60-898b-5bc8142250c6\"]}")
    public ResponseResult inputs(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.get("ids")).isPresent()) {
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.inputs(jsonObject);
    }

    @RequiresAuthentication
    @PostMapping("/input2")
    @LogAnotion(type = "转入",value = "1",incident = "个人信息",prefixContent = "转入信息")
    @ApiOperation(value = "导入人员修改中多个转入", httpMethod = "POST", notes = "{\"ids\":[\"255762da-3072-4b60-898b-5bc8142250c6\"]}")
    public ResponseResult input2(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.get("ids")).isPresent()) {
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.input2(jsonObject);
    }

    @RequiresAuthentication
    @PostMapping(value = "/inlistnopage")
    @ApiOperation(value = "导入人员-不分页列表查询", httpMethod = "POST", notes = "")
    public ResponseResult inlist2(@RequestBody JSONObject condition) {
        JSONObject condition1 = condition.getJSONObject("condition");
        String s = JSONObject.toJSONString(condition1);
        return personService.selectPage4(s);
    }

    @RequiresAuthentication
    @PostMapping(value = "/genum")
    @ApiOperation(value = "导入人员-获取完整人员数量", httpMethod = "POST", notes = "")
    public ResponseResult geNumber() {
        return personService.getNumber();
    }

    @RequiresAuthentication
    @PostMapping("/updates")
    @LogAnotion(type = "批量保存导入人员",value = "1",incident = "个人信息",prefixContent = "批量保存导入人员")
    @ApiOperation(value = "批量保存导入人员", httpMethod = "POST", notes = "{\"ids\":[\"255762da-3072-4b60-898b-5bc8142250c6\"]}")
    public ResponseResult updates(@RequestBody JSONObject jsonObject, HttpServletRequest request) {

        if (!Optional.ofNullable(jsonObject.get("ids")).isPresent()) {
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.updates(jsonObject,request);
    }

    @PostMapping("/deleteinportbyId")
    @LogAnotion(type = "导入人员删除",value = "1",incident = "个人信息",prefixContent = "")
    @ApiOperation(value = "导入人员删除", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\"}")
    public ResponseResult deleteInportById(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.getString("id")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return personService.deleteInportById(jsonObject.getString("id"));
    }


    @PostMapping("/132")
    @ApiOperation(value = "此接口无实际作用,前端需要字段参照,联调完成之后可删", httpMethod = "POST", notes = "{\"password\":\"123456\",\"role\":\"123456\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"123\",\"465\"],\"username\":\"123456\"}")
    public ResponseResult save1(@RequestBody Person sysRole) {

        return null;
    }

    @PostMapping("/1312")
    @ApiOperation(value = "此接口无实际作用,前端需要字段参照,联调完成之后可删", httpMethod = "POST", notes = "{\"password\":\"123456\",\"role\":\"123456\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"123\",\"465\"],\"username\":\"123456\"}")
    public ResponseResult save2(@RequestBody PersonQuery sysRole) {

        return null;
    }


    @PostMapping("/13123")
    @ApiOperation(value = "此接口无实际作用,前端需要字段参照,联调完成之后可删", httpMethod = "POST", notes = "{\"password\":\"123456\",\"role\":\"123456\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"123\",\"465\"],\"username\":\"123456\"}")
    public ResponseResult save2(@RequestBody InportLog sysRole) {

        return null;
    }

}
